package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Result;
import model.User;
import service.impl.JSONDecoder;
import dao.ResultDAO;

public class ResultData extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ResultData() {
		super();
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		User user = (User) (session.getAttribute("currentSessionUser"));
		try {
			String action = request.getParameter("action");
			if (action.equals("save")) {
				String results = request.getParameter("results");
				List<Result> userResults = JSONDecoder.getUserResults(results, user.getId());
				ResultDAO.save(userResults);
			}
		} catch (Throwable theException) {
			System.out.println(theException);
		}
	}
}